//
//  ZLTabViewController.m
//  LittleFairyPoet
//
//  Created by rimi on 15/9/8.
//  Copyright (c) 2015年 周林. All rights reserved.
//

#import "ZLTabViewController.h"
#import "UIImage+Clips.h"
#import "AMWaveTransition.h"
#import <DCPathButton.h>
static CGFloat const buttonW = 140;
static CGFloat const buttonH = 35;

@interface ZLTabViewController () <DCPathButtonDelegate, UINavigationControllerDelegate> {
    
    NSInteger _count;
    NSMutableArray *_dataSource;
    NSMutableArray *_poemSource;
}

@property (nonatomic, strong) NSArray * titles;
@property (nonatomic, strong) NSArray * controllers;
@property (nonatomic, strong) UILabel *poemTitle; /**< 诗名 */
@property (nonatomic, strong) UILabel *poetName; /**< 诗人 */
@property (nonatomic, strong) UILabel *firstPoem; /**< 第1句诗 */
@property (nonatomic, strong) UILabel *secondPoem; /**< 第2句诗 */
@property (nonatomic, strong) UILabel *thirdPoem; /**< 第3句诗 */
@property (nonatomic, strong) UILabel *fourthPoem; /**< 第4句诗 */
@property (nonatomic, strong) DCPathButton *menueButton /**< 菜单 */;
@property (nonatomic, strong) AMWaveTransition *interactive /**<  */;
@property (nonatomic, strong) UIImageView *yanziImageView /**< 燕子动画 */;
@property (nonatomic, strong) UIButton *gameButton /**< 游戏按钮 */;
@property (nonatomic, strong) UIButton *learnButton /**< 学习按钮 */;
@property (nonatomic, strong) UIButton *collectionButton /**< 收藏按钮 */;
@property (nonatomic, strong) UIButton *planButton /**< 计划按钮 */;
@property (nonatomic, strong) UIButton *assistantButton /**< 帮助按钮 */;

- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation ZLTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.navigationController setDelegate:self];
    [self.interactive attachInteractiveGestureToNavigationController:self.navigationController];
    [_yanziImageView startAnimating];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.interactive detachInteractiveGesture];
}

#pragma mark - init
- (void)initializeDataSource {
    _count = 0;
    //获取plist的bundle路径
    NSString *plistPath = [[NSBundle mainBundle] pathForAuxiliaryExecutable:@"绝句.plist"];
    //_tapPoemSource = [NSMutableArray array];
    _dataSource = [NSMutableArray array];
    NSMutableArray *arr1 = [NSMutableArray arrayWithArray:[NSArray arrayWithContentsOfFile:plistPath]];
    NSMutableArray *arr2 = [NSMutableArray arrayWithArray:[self randomFromNumber:0 toNumber:arr1.count-1]];
    for (int i = 0; i < arr1.count; i++) {
        [_dataSource addObject:arr1[[arr2[i] integerValue]]];
    }
    [self poemSegementation];
}

- (void)initializeUserInterface {
    self.view.backgroundColor = [UIColor whiteColor];
    CALayer *layer = [CALayer layer];
    layer.bounds = self.view.bounds;
    layer.position = self.view.center;
    layer.contents = (__bridge id)(IMAGE_CONTENT(@"背景图.png")).CGImage;
    [self.view.layer addSublayer:layer];
    [self.navigationController setNavigationBarHidden:YES];
    self.leftButton.hidden = YES;
    
    [self.view addSubview:self.poemTitle];
    [self.view addSubview:self.poetName];
    [self.view addSubview:self.firstPoem];
    [self.view addSubview:self.secondPoem];
    [self.view addSubview:self.thirdPoem];
    [self.view addSubview:self.fourthPoem];
    [self.view addSubview:self.learnButton];
    [self.view addSubview:self.collectionButton];
    [self.view addSubview:self.gameButton];
    [self.view addSubview:self.planButton];
    [self.view addSubview:self.assistantButton];
    [self yanziAnimate];
    [self.view addSubview:_yanziImageView];
    [UIView animateWithDuration:1 animations:^{
        self.poemTitle.alpha = 1;
        self.poetName.alpha = 1;
        self.gameButton.frame = DHFlexibleFrame(CGRectMake(10, 370, buttonW, buttonH), NO);
        self.learnButton.frame = DHFlexibleFrame(CGRectMake(170, 370, buttonW, buttonH), NO);
        self.planButton.frame = DHFlexibleFrame(CGRectMake(10, 430, buttonW, buttonH), NO);
        self.collectionButton.frame = DHFlexibleFrame(CGRectMake(170, 430, buttonW, buttonH), NO);
        self.assistantButton.center = DHFlexibleCenter(CGPointMake(ORIGIN_WIDTH / 2, 420));
    } completion:^(BOOL finished) {
        [self firstPoemAnimationBlock];
    }];
    
}

#pragma mark - private
- (void) yanziAnimate {
    _yanziImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 110, ORIGIN_WIDTH - 20, 100) adjustWidth:NO];
    NSMutableArray *animateArray = [NSMutableArray array];
    for (NSUInteger i = 1; i < 25 ; i ++) {
        NSString *imageName = [NSString stringWithFormat:@"jj_y%ld.png", i];
        [animateArray addObject:IMAGE_CONTENT(imageName)];
    }
    _yanziImageView.animationImages = animateArray;
    _yanziImageView.animationDuration = 3.0;
    _yanziImageView.animationRepeatCount = HUGE_VAL;
}
//将plist中的诗乱序且不重复
- (NSMutableArray *)randomFromNumber:(NSInteger)fromNumber toNumber:(NSInteger)toNumber {
    NSMutableArray *arr1 = [NSMutableArray array];
    for (NSInteger i = fromNumber; i <= toNumber; i++) {
        [arr1 addObject:@(i)];
    }
    for (int i = 0; i <= 100; i++) {
        NSInteger value = arc4random() % (toNumber - fromNumber + 1);
        [arr1 exchangeObjectAtIndex:value withObjectAtIndex:0];
    }
    return arr1;
}

//将一首诗分成4句装入数组中
- (void)poemSegementation {
    _poemSource = [NSMutableArray array];
    NSMutableString *string = [_dataSource[_count][@"content"] mutableCopy];
    NSString *temp = nil;
    int j = 0;
    for (int i = 0; i < string.length; i++) {
        for (j = i; j < string.length; j++) {
            temp = [string substringWithRange:NSMakeRange(j, 1)];
            if ([temp isEqualToString:@"，"] || [temp isEqualToString:@"？"] || [temp isEqualToString:@"。"] || [temp isEqualToString:@"；"] || [temp isEqualToString:@"！"]) {
                [_poemSource addObject:[string substringWithRange:NSMakeRange(i, j - i)]];
                j = j + 2;
                i = j;
            }
        }
    }
}

- (void)firstPoemAnimationBlock {
    [UIView animateWithDuration:1 animations:^{
        self.firstPoem.alpha = 1;
    } completion:^(BOOL finished) {
        [self secondPoemAnimationBlock];
    }];
}

- (void)secondPoemAnimationBlock {
    [UIView animateWithDuration:1 animations:^{
        self.secondPoem.alpha = 1;
        self.menueButton.alpha = 1;
    } completion:^(BOOL finished) {
        [self thirdPoemAnimationBlock];
    }];
}

- (void)thirdPoemAnimationBlock {
    [UIView animateWithDuration:1 animations:^{
        self.thirdPoem.alpha = 1;
    } completion:^(BOOL finished) {
        [self fourthPoemAnimationBlock];
    }];
}

- (void)fourthPoemAnimationBlock {
    [UIView animateWithDuration:1 animations:^{
        self.fourthPoem.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}


#pragma mark - responds events

- (void)pathButton:(DCPathButton *)dcPathButton clickItemButtonAtIndex:(NSUInteger)itemButtonIndex {
    Class class = NSClassFromString(self.controllers[itemButtonIndex]);
    UIViewController * VC = [[class alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void) respondsToButton :(UIButton *) button {
    
    Class class = NSClassFromString(self.controllers[button.tag]);
    UIViewController * VC = [[class alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - protocol
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController*)fromVC
                                                 toViewController:(UIViewController*)toVC
{
    if (operation != UINavigationControllerOperationNone) {
        return [AMWaveTransition transitionWithOperation:operation andTransitionType:AMWaveTransitionTypeNervous];
    }
    return nil;
}

- (void)dealloc
{
    [self.navigationController setDelegate:nil];
}


#pragma mark - getter

- (NSArray *)controllers {
    if (!_controllers) {
        _controllers = @[@"ZLGameViewController",
                         @"DirectoryViewController",
                         @"ZLScheduleViewController",
                         @"CollectionViewController",
                         @"WPGuideViewController"];
    }
    return _controllers;
}

- (UILabel *)poemTitle {
    if (!_poemTitle) {
        _poemTitle = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 110,260, 60) adjustWidth:YES];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = _dataSource[_count][@"title"];
            label.textColor = [UIColor darkGrayColor];
            label.font = [UIFont fontWithName:@"Kaiti" size:20];
            label.alpha = 0;
            label.numberOfLines = 0;
            label;
        });
    }
    return _poemTitle;
}

- (UILabel *)poetName {
    if (!_poetName) {
        _poetName = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 160, 80, 30) adjustWidth:YES];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = _dataSource[_count][@"zuozhe"];
            label.textColor = [UIColor darkGrayColor];
            label.font = [UIFont fontWithName:@"Kaiti" size:20];
            label.alpha = 0;
            label;
        });
    }
    return _poetName;
}

- (UILabel *)firstPoem {
    if (!_firstPoem) {
        _firstPoem = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 190, 220, 45) adjustWidth:YES];
            label.textAlignment = NSTextAlignmentCenter;
            NSMutableString *string = [_poemSource[0] mutableCopy];
            label.text = string;
            label.textColor = [UIColor darkGrayColor];
            label.font = [UIFont fontWithName:@"Kaiti" size:20];
            label.alpha = 0;
            label;
        });
    }
    return _firstPoem;
}

- (UILabel *)secondPoem {
    if (!_secondPoem) {
        _secondPoem = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 220, 220, 45) adjustWidth:YES];
            label.textAlignment = NSTextAlignmentCenter;
            NSMutableString *string = [_poemSource[1] mutableCopy];
            label.text = string;
            label.textColor = [UIColor darkGrayColor];
            label.font = [UIFont fontWithName:@"Kaiti" size:20];
            label.alpha = 0;
            label;
        });
    }
    return _secondPoem;
}

- (UILabel *)thirdPoem {
    if (!_thirdPoem) {
        _thirdPoem = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 250, 220, 45) adjustWidth:YES];
            label.textAlignment = NSTextAlignmentCenter;
            NSMutableString *string = [_poemSource[2] mutableCopy];
            label.text = string;
            label.textColor = [UIColor darkGrayColor];
            label.font = [UIFont fontWithName:@"Kaiti" size:20];
            label.alpha = 0;
            label;
        });
    }
    return _thirdPoem;
}

- (UILabel *)fourthPoem {
    if (!_fourthPoem) {
        _fourthPoem = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 280, 220, 45) adjustWidth:YES];
            label.textAlignment = NSTextAlignmentCenter;
            NSMutableString *string = [_poemSource[3] mutableCopy];
            label.text = string;
            label.textColor = [UIColor darkGrayColor];
            label.font = [UIFont fontWithName:@"Kaiti" size:20];
            label.alpha = 0;
            label;
        });
    }
    return _fourthPoem;
}

- (UIButton *)gameButton {
    if (!_gameButton) {
        _gameButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _gameButton.frame = DHFlexibleFrame(CGRectMake(-150, 390, buttonW, buttonH), NO);
        [_gameButton setImage:IMAGE_WITH_NAME(@"游戏.png") forState:UIControlStateNormal];
        _gameButton.tag = 0;
        _gameButton.adjustsImageWhenHighlighted = NO;
        [_gameButton addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _gameButton;
}

- (UIButton *)learnButton {
    if (!_learnButton) {
        _learnButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _learnButton.frame = DHFlexibleFrame(CGRectMake(320, 390, buttonW, buttonH), NO);
        [_learnButton setImage:IMAGE_WITH_NAME(@"学习.png") forState:UIControlStateNormal];
        _learnButton.tag = 1;
        _learnButton.adjustsImageWhenHighlighted = NO;
        [_learnButton addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _learnButton;
}

- (UIButton *)planButton {
    if (!_planButton) {
        _planButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _planButton.frame = DHFlexibleFrame(CGRectMake(-150, 470, buttonW, buttonH), NO);
        [_planButton setImage:IMAGE_WITH_NAME(@"计划.png") forState:UIControlStateNormal];
        _planButton.tag = 2;
        _planButton.adjustsImageWhenHighlighted = NO;
        [_planButton addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _planButton;
}

- (UIButton *)collectionButton {
    if (!_collectionButton) {
        _collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _collectionButton.frame = DHFlexibleFrame(CGRectMake(320, 470, buttonW, buttonH), NO);
        [_collectionButton setImage:IMAGE_WITH_NAME(@"收藏.png") forState:UIControlStateNormal];
        _collectionButton.tag = 3;
        _collectionButton.adjustsImageWhenHighlighted = NO;
        [_collectionButton addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _collectionButton;
}

- (UIButton *)assistantButton {
    if (!_assistantButton) {
        _assistantButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _assistantButton.frame = DHFlexibleFrame(CGRectMake(-150, ORIGIN_HEIGHT, buttonW, buttonH), NO);
        _assistantButton.center = DHFlexibleCenter(CGPointMake(ORIGIN_WIDTH / 2, 540));
        [_assistantButton setImage:IMAGE_WITH_NAME(@"帮助.png") forState:UIControlStateNormal];
        _assistantButton.tag = 4;
        _assistantButton.adjustsImageWhenHighlighted = NO;
        [_assistantButton addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _assistantButton;
}


@end
